<?php
class BBCode
{
    
    private $bbcode = array();
    
    
    public function __construct()
    {
        $query = 'SELECT
                    bbcode.search,
                    bbcode.replace
                  FROM `'.TABLEPREFIX.'bbcode` AS bbcode
                  WHERE bbcode.active=1';
        $this->db->prepare($query);
        $this->db->execute();
        $array = $this->db->fetchAll();
        $this->db->free();
        
        $preg = array();
        $preg['{URL}'] = '((?:(?:[a-z]+):\\/\\/)?(?:[^\\/:]+)(?::(?:\\d{1,5}))?(?:\\/[^\\#]*)?(?:\\#(?:.*))?)';
        $preg['{TEXT}'] = '(.+)';
        $preg['{SIMPLETEXT}'] = '([A-Za-z0-9 \\,\\.\\-\\+_]+)';
        $preg['{COLOR}'] = '(\\#[0-9]{3}|\\#[0-9]{6})';
        $preg['{NUMBER}'] = '([0-9]+)';
        
        unset($this->bbcode);        
        foreach ($array as $value)
        {
            preg_match_all('#{(URL|TEXT|SIMPLETEXT|COLOR|NUMBER)[0-9]*}#',$value['search'],$search);
			foreach ($search[0] as $key => $val)
            {
                $this->bbcode[str_replace($val,$preg[$val],$value['search'])] = str_replace($val,'$'.($key+1),$value['replace']);
            }
        }
    }    
    
    
    
    
    
    public function bbcode($text,$unhtml = TRUE,$nl2br = TRUE)
    {
        global $core;
        if ($unhtml)
        {
            $text = $core->html($text,$nl2br);
        }
        return preg_replace(array_keys($this->bbcode),array_values($this->bbcode),$text);
    }
    
    
    
    
    
    public function buttons()
    {
        
    }
}
?>